/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.dp1.pucp.Produccion.Vista;

import com.dp1.pucp.Modelo.Algoritmos.EmpleadoAlgoritmo;
import com.dp1.pucp.Modelo.Clases.Empleadoxmaquinasimulacion;
import com.dp1.pucp.Modelo.Clases.Simulacion;
import com.dp1.pucp.Modelo.Clases.Trabajo;
import com.dp1.pucp.Util.Validaciones;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.JFrame;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Rodrigo
 */
public class RegResultadosGUI extends javax.swing.JDialog {
    
    private JFrame padre;

    /**
     * Creates new form RegResultadosGUI
     */
    public RegResultadosGUI(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        setLocationRelativeTo(null);
    }
    
    public JFrame getPadre(){ return padre; }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        txtTurno = new javax.swing.JTextField();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblAsignaciones = new javax.swing.JTable();
        btnGuardar = new javax.swing.JButton();
        btnCerrar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Registro Resultados de Produccion"));

        jLabel1.setText("Turno");

        txtTurno.setText("jTextField1");

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Asignaciones"));

        tblAsignaciones.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null}
            },
            new String [] {
                "Operario", "Producto", "TipoMaquina", "Producido", "Roto", "Producido Teorico", "Roto Teorico"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Object.class, java.lang.Object.class, java.lang.Object.class, java.lang.Integer.class, java.lang.Integer.class, java.lang.Object.class, java.lang.Object.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, true, true, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane1.setViewportView(tblAsignaciones);
        tblAsignaciones.getColumnModel().getColumn(3).setPreferredWidth(40);
        tblAsignaciones.getColumnModel().getColumn(4).setPreferredWidth(40);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 665, Short.MAX_VALUE)
                .addGap(9, 9, 9))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        btnGuardar.setFont(new java.awt.Font("Ubuntu", 0, 10)); // NOI18N
        btnGuardar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/save.png"))); // NOI18N
        btnGuardar.setText("Guardar");
        btnGuardar.setBorderPainted(false);
        btnGuardar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnGuardar.setMaximumSize(new java.awt.Dimension(57, 29));
        btnGuardar.setMinimumSize(new java.awt.Dimension(57, 29));
        btnGuardar.setPreferredSize(new java.awt.Dimension(57, 29));
        btnGuardar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);

        btnCerrar.setFont(new java.awt.Font("Ubuntu", 0, 10)); // NOI18N
        btnCerrar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/Logout.png"))); // NOI18N
        btnCerrar.setText("Cerrar");
        btnCerrar.setBorderPainted(false);
        btnCerrar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnCerrar.setMaximumSize(new java.awt.Dimension(57, 29));
        btnCerrar.setMinimumSize(new java.awt.Dimension(57, 29));
        btnCerrar.setPreferredSize(new java.awt.Dimension(57, 29));
        btnCerrar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(txtTurno, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addContainerGap(601, Short.MAX_VALUE))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGap(0, 0, Short.MAX_VALUE)
                                .addComponent(btnGuardar, javax.swing.GroupLayout.PREFERRED_SIZE, 128, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(105, 105, 105)
                                .addComponent(btnCerrar, javax.swing.GroupLayout.PREFERRED_SIZE, 128, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(180, 180, 180))))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addContainerGap())))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(23, 23, 23)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txtTurno, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btnGuardar, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnCerrar, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(RegResultadosGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(RegResultadosGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(RegResultadosGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(RegResultadosGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the dialog */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                RegResultadosGUI dialog = new RegResultadosGUI(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    
    public void addGuardarListener(ActionListener ml){ this.btnGuardar.addActionListener(ml); }
//    public void addExaminarListener(ActionListener ml){ this.btnExaminar.addActionListener(ml); }
    public void addCerrarListener(ActionListener ml){ this.btnCerrar.addActionListener(ml); }
    
    
    
    public JTable getTabla(){
        return tblAsignaciones;
    }
    
    public int getFilaSelAsignacion(){ return tblAsignaciones.getSelectedRow(); }
    
    
    public String nombreProductoSimulacion(int idEmpleado, ArrayList<Empleadoxmaquinasimulacion> listaEmpleadoxMaquina){
        String nombre=null;
        for (int i=0; i<listaEmpleadoxMaquina.size();i++){
            if (listaEmpleadoxMaquina.get(i).getEmpleado().getIdempleado()==idEmpleado){
                nombre=listaEmpleadoxMaquina.get(i).getTipoproducto().getNombre();
            }
        }
        return nombre;
    }
    
    public void setTurno(int turno){
        
        if (turno==1){
            txtTurno.setText("Mañana");
        }
        if (turno==2){
            txtTurno.setText("Tarde");
        }
        if (turno==3){
            txtTurno.setText("Noche");
        }
        txtTurno.setEnabled(false);
    }
    
    public void mostrarAviso(String msn) {
        Validaciones.mostrarMensajeAvisoVal(msn);
    }

    public void mostrarError(String msn) {
        Validaciones.mostrarMensajeErrorVal(msn);
    }
    
    private EmpleadoAlgoritmo getEmpleadoAlgoritmo(ArrayList<EmpleadoAlgoritmo> arreglo, int id){
        EmpleadoAlgoritmo emp=new EmpleadoAlgoritmo();
        for (int i=0; i<arreglo.size();i++){
            if (arreglo.get(i).getIdempleado()==id){
                emp=arreglo.get(i);
            }
        }
        return emp;
    }
    
    
    
    public void llenaGrilla(Simulacion simulacion,ArrayList<Empleadoxmaquinasimulacion> listaEmpleadoxMaquina,ArrayList<EmpleadoAlgoritmo> arregloEmpleados){        
        if(listaEmpleadoxMaquina!=null){ 
            ((DefaultTableModel) this.tblAsignaciones.getModel()).setRowCount(0);
            for (int i = 0; i<listaEmpleadoxMaquina.size();i++){
                ((DefaultTableModel) tblAsignaciones.getModel()).setRowCount(tblAsignaciones.getRowCount()+1);
                
                String nombre = listaEmpleadoxMaquina.get(i).getEmpleado().getNombre()+" "+listaEmpleadoxMaquina.get(i).getEmpleado().getApPaterno()+" "+listaEmpleadoxMaquina.get(i).getEmpleado().getApMaterno();
                tblAsignaciones.setValueAt(nombre,i,0);
                String nombreProducto=nombreProductoSimulacion(listaEmpleadoxMaquina.get(i).getEmpleado().getIdempleado(),listaEmpleadoxMaquina);
                tblAsignaciones.setValueAt(nombreProducto,i,1);
                int id=listaEmpleadoxMaquina.get(i).getMaquina().getTipomaquina().getIdtipomaquina();
                switch(id){
                    case 1:
                         tblAsignaciones.setValueAt("Horneado",i,2); 
                        break;
                    case 2:
                        tblAsignaciones.setValueAt("Decorado",i,2); 
                        break;
                    case 3:
                        tblAsignaciones.setValueAt("Rellenado",i,2); 
                        break;
                    case 4:
                        tblAsignaciones.setValueAt("Empaque",i,2); 
                }
                EmpleadoAlgoritmo emp=getEmpleadoAlgoritmo(arregloEmpleados,listaEmpleadoxMaquina.get(i).getEmpleado().getIdempleado());
                tblAsignaciones.setValueAt((emp.getProduccion()[id-1])*30,i,5); 
                tblAsignaciones.setValueAt((emp.getMermas()[id-1])*30,i,6); 
             }           
        }
   }
    
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCerrar;
    private javax.swing.JButton btnGuardar;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tblAsignaciones;
    private javax.swing.JTextField txtTurno;
    // End of variables declaration//GEN-END:variables
}
